package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.google.android.material.progressindicator.CircularProgressIndicator;
import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;
import com.sense.kukoo.lib_picture.loader.Monet;

public class MediaListItemView  extends FrameLayout {

    private static final String TAG = MediaListItemView.class.getSimpleName();

    private View root;
    private View content;
    private CardView card_view;
    private ImageView iv_music_icon;
    private ImageView iv_btn_play;
    private CircularProgressIndicator iv_btn_progress;
    private TextView tv_title;
    private TextView tv_collection_title;
    private TextView tv_time_total;
    private TextView bg_btn_to_list;
    private View btn_to_list;
    private TextView tv_music_number_right;
    private TextView tv_music_number_left;
    private View group_to_list;
    private ImageView iv_btn_delete;

    private String iconUrl;

    public MediaListItemView(@NonNull Context context) {
        super(context);
        initView();
    }

    public MediaListItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MediaListItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public MediaListItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView(){
        LayoutInflater.from(getContext()).inflate(R.layout.common_item_media, this, true);
        //
        root = findViewById(R.id.root);
        content = findViewById(R.id.content);
        card_view = findViewById(R.id.card_view);
        iv_music_icon = findViewById(R.id.iv_music_icon);
        iv_btn_play = findViewById(R.id.iv_btn_play);
        iv_btn_progress = findViewById(R.id.iv_btn_progress);
        tv_title = findViewById(R.id.tv_title);
        tv_collection_title = findViewById(R.id.tv_collection_title);
        tv_time_total = findViewById(R.id.tv_time_total);
        bg_btn_to_list = findViewById(R.id.bg_btn_to_list);
        btn_to_list = findViewById(R.id.btn_to_list);
        tv_music_number_right = findViewById(R.id.tv_music_number_right);
        tv_music_number_left = findViewById(R.id.tv_music_number_left);
        group_to_list = findViewById(R.id.group_to_list);
        iv_btn_delete = findViewById(R.id.iv_btn_delete);
        //
        content.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow) {
                    isShow = false;
                    dissmissDeleteBtn();
                    return;
                }
                if (mListener!=null) {
                    mListener.onItemClick();
                }
            }
        });
        content.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mListener!=null) {
                    mListener.onItemLongClick(v);
                }
                return true;
            }
        });
        iv_btn_play.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow) {
                    isShow = false;
                    dissmissDeleteBtn();
                    return;
                }
                if (mListener!=null) {
                    mListener.onBtnPlayClick();
                }
            }
        });
        btn_to_list.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow) {
                    isShow = false;
                    dissmissDeleteBtn();
                    return;
                }
                if (mListener!=null) {
                    mListener.onBtnToListClick();
                }
            }
        });
        iv_btn_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null) {
                    mListener.onItemDeleteClick();
                }
            }
        });
        //item的删除按钮先去掉
        //initTouchEvent();
    }

    public void setIcon(String url){
        if (!url.equals(iconUrl)) {
            Monet.get(ApplicationContext.instance())
                    .load(url)
                    .placeholder(R.mipmap.common_icon_default_editor_picks)
                    .into(iv_music_icon);
        }
    }

    public void setTitle(String text){
        tv_title.setText(text);
    }

    public void setCollectionTitle(String text){
        tv_collection_title.setText(text);
    }

    public void setTotalTime(String text){
        tv_time_total.setText(text);
    }

    public void setNumberLeft(String text){
        tv_music_number_left.setText(text);
    }
    public void setNumberLeftColor(@ColorInt int color){
        tv_music_number_left.setTextColor(color);
    }

    public void setNumberRight(String text){
        tv_music_number_right.setText(text);
    }

    public void setToListVisiable(boolean isShow){
        group_to_list.setVisibility(isShow ? View.VISIBLE: View.GONE);
    }

    public void setProgressVisiable(boolean isShow){
        iv_btn_progress.setVisibility(isShow ? View.VISIBLE: View.INVISIBLE);
    }

    public void setProgress(int progress){
        iv_btn_progress.setProgress(progress);
    }

    public void setPlayIcon(@DrawableRes int resId){
        iv_btn_play.setImageResource(resId);
    }

    private OnMediaListItemEventListener mListener;

    public interface OnMediaListItemEventListener{
        void onItemClick();
        void onItemLongClick(View view);
        void onItemDeleteClick();
        void onBtnPlayClick();
        void onBtnToListClick();
        void onTouchChanged(boolean isTouching);
    }

    public void setListener(OnMediaListItemEventListener l){
        mListener = l;
    }

    private long downTime = -1L;
    private float touchX = Float.MIN_VALUE;
    private float touchY = Float.MIN_VALUE;
    private float translationX = 0F;
    private boolean isShow = false;
    private float mDeleteBtnWith = -1F;
    private boolean isTouching = false;

    private void setIsTouching(boolean b){
        if (isTouching != b) {
            isTouching = b;
            if (mListener!=null) {
                mListener.onTouchChanged(isTouching);
            }
        }
    }

    private void onTouchStart(Float x){
        setIsTouching(true);
        float moveX = x - touchX;
        translationX += moveX;
        if (isShow) {
            content.setTranslationX(translationX -mDeleteBtnWith);
            iv_btn_delete.setTranslationX(translationX);
        }else{
            content.setTranslationX(translationX);
            iv_btn_delete.setTranslationX(mDeleteBtnWith + translationX);
        }
    }

    private void onTouchFinish(Float x){
        setIsTouching(false);
        if (isShow) {
            isShow = translationX < (mDeleteBtnWith * 0.4);
        }else{
            isShow = translationX < (-(mDeleteBtnWith * 0.6));
        }
        if (isShow) {
            showDeleteBtn();
        }else{
            dissmissDeleteBtn();
        }
        //复位
        translationX = 0F;
    }

    private void showDeleteBtn(){
        content.animate().translationX(-mDeleteBtnWith).setDuration(300).start();
        iv_btn_delete.animate().translationX(0F).setDuration(300).start();
    }

    private void dissmissDeleteBtn(){
        content.animate().translationX(0F).setDuration(300).start();
        iv_btn_delete.animate().translationX(mDeleteBtnWith).setDuration(300).start();
    }

    private void initTouchEvent(){
        content.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                long time = 0;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        if(downTime<0){
                            downTime = event.getEventTime();
                        }
                        if(touchX == Float.MIN_VALUE){
                            touchX = event.getX();
                        }
                        if(touchY == Float.MIN_VALUE){
                            touchY = event.getY();
                        }
                        if(mDeleteBtnWith < 0){
                            mDeleteBtnWith = iv_btn_delete.getWidth() + 0F;
                        }
                        onTouchStart(event.getX());
                        break;


                    case MotionEvent.ACTION_UP:
                        onTouchFinish(event.getX());
                        time = event.getEventTime() - downTime;
                        float moveX = Math.abs(event.getX() - touchX);
                        float moveY = Math.abs(event.getY() - touchY);
                        if(time<150 && (moveX < 100) && (moveY < 100)){
                            v.performClick();
                        }
                        //
                        touchX = Float.MIN_VALUE;
                        touchY = Float.MIN_VALUE;
                        downTime = -1;
                        break;

                    case MotionEvent.ACTION_CANCEL:
                        onTouchFinish(event.getX());
                        //
                        touchX = Float.MIN_VALUE;
                        touchY = Float.MIN_VALUE;
                        downTime = -1;
                        break;
                }
                return true;
            }
        });
    }
}
